using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace MTRC.ODMS.Web.SOM.CodeSetup
{
    /// <summary>
    /// author:Zeng shunbo
    /// </summary>
    public partial class AssetTypeEntry : BasePage_SOM
    {
        #region MenuID

        protected override string MenuID
        {
            get
            {
                return "M140801";
            }
        }

        #endregion

        #region Some protected field

        protected int assetTypeID;
        protected int locationID;
        protected string assetTypeCode;
        protected string assetTypeDesc;
        protected int flagObsolete;
        protected int createUser;
        protected int lastUpdUser;
        protected int actingasUser;

        #endregion

        #region Page Init And Load

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ChangeLanguage += new ChangeLanguageEventHandler(AssetTypeEntry_ChangeLanguage);
            AddClienScript();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.TracePageChange = true;//alert when change page.

            if (!IsPostBack)
            {
                SetLanguage();
                Display();
            }
            else
            {
                this.IsServerChanged = true;
            }
        }

        #endregion

        #region SetLanguage

        void AssetTypeEntry_ChangeLanguage(object sender, EventArgs e)
        {
            SetLanguage();
            if (IsPostBack)  //added by Huangyao 20090119
            {
                Display();
            }
        }

        private void SetLanguage()
        {
            //Set label or button text.
            this.lblMenuCaption.Text = GetLabel("SOM_AssetTypeEntry");
            this.lblAssetType.Text = GetLabel("SOM_AssetType", true);
            this.lblObsolete.Text = GetLabel("SOM_Void", true);
            this.lblStation.Text = GetLabel("SOM_Station", true);
            this.lblDescription.Text = GetLabel("COM_Description", true);
            this.btnSave.Text = GetLabel("COM_Save");
            this.btnSaveAndNew.Text = GetLabel("SOM_SaveNew");
            this.btnSaveAndReturn.Text = GetLabel("COM_SaveAndReturn");
            this.btnCancel.Text = GetLabel("COM_Cancel");
        }

        #endregion

        #region Some private Function

        //regist script to Client.
        private void AddClienScript()
        {
            this.btnSave.Attributes.Add("onclick", "return CheckVoid(\"" + this.GetMessage("SOM_AssetTypeVoidConfirm") +"\",'" +CheckVoid() + "');");
            this.btnSaveAndNew.Attributes.Add("onclick", "return CheckVoid('" + this.GetMessage("SOM_AssetTypeVoidConfirm") + "','" + CheckVoid() + "');");
            this.btnSaveAndReturn.Attributes.Add("onclick", "return CheckVoid('" + this.GetMessage("SOM_AssetTypeVoidConfirm") + "','" + CheckVoid() + "');");
        }

        private void Display()
        {
            FunctionItemSetting();

            //Set default value of Text.
            if ( Request.QueryString["AssetTypeID"] != null && Request.QueryString["action"].ToUpper() == "EDIT")//Edit
            {
                DataTable assetTypeInfo = DAL.SOM.CodeSetup.CDAssetType.GetAssetTypeWithID(Convert.ToInt32(Request.QueryString["AssetTypeID"]));

                //this.lblAssetTypeID.Text = assetTypeInfo.Rows[0]["AssetTypeID"].ToString();

                //if (ddlStation.Items.FindByValue(assetTypeInfo.Rows[0]["LocationID"].ToString()) != null) //Modify at:2008-11-21
                //{
                //    this.ddlStation.SelectedValue = assetTypeInfo.Rows[0]["LocationID"].ToString();
                //}

                FindByValueAndSelect(this.ddlStation, assetTypeInfo.Rows[0]["LocationID"].ToString());//add at:2008-11-21

                this.ddlStation.Enabled = false;
                this.txtAssetType.Text = assetTypeInfo.Rows[0]["AssetTypeCode"].ToString();
                this.txtDescription.Text = assetTypeInfo.Rows[0]["AssetTypeDesc"].ToString();
                this.chkObsolete.Checked = assetTypeInfo.Rows[0]["FlagObsolete"].ToString() == "False" ? false : true;

                if (assetTypeInfo.Rows[0]["FlagObsolete"].ToString() == "True")
                {
                    DisableAllControls();
                }

                assetTypeInfo.Dispose();
                assetTypeInfo = null;
                //this.btnSaveAndNew.Enabled = false;
            }
            else if (Request.QueryString["action"]!=null && Request.QueryString["action"].ToUpper() == "NEW")//New
            {
                //update by gordon 20151021 U247
                //if (this.ddlStation.Items.FindByValue(CurrentStation.ToString()) != null)
                //{
                //    this.ddlStation.SelectedValue = this.CurrentStation.ToString();
                //}
                if (this.ddlStation.Items.FindByValue(CurrentLocation.ToString()) != null)
                {
                    this.ddlStation.SelectedValue = this.CurrentLocation.ToString();
                }

                this.chkObsolete.Enabled = false;//It is display only.
            }
            else
            {
                Response.Redirect("AssetTypeList.aspx");
            }

           
        }

        /// <summary>
        /// setting user access by function item.
        /// </summary>
        private void FunctionItemSetting()
        {
            if (DAL.Security.User.CanFunctionItemAccess("M140801_F1", this.ODMUser.UserProfileID, this.MenuID))//access all station.
            {
                //update by gordon 20151020 U247
                //BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, true, Convert.ToInt16(LangCode), this.ODMUser.StationID), false);
                BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, true, Convert.ToInt16(LangCode), this.ODMUser.LocationID), false);
            }
            else if (DAL.Security.User.CanFunctionItemAccess("M140801_F4", this.ODMUser.UserProfileID, this.MenuID))//Can Access other stations.
            {
                //BindStation
                //update by gordon 20151020 U247
                //BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.StationID), false);
                BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.LocationID), false);
            }
            else
            {
                //update by gordon 20151020 U247
                //BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.StationID), false);
                BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.LocationID), false);
                this.ddlStation.Enabled = false;
            }

            if (DAL.Security.User.CanFunctionItemAccess("M140801_F2", this.ODMUser.UserProfileID, this.MenuID)) //readonly
            {
                DisableAllControls();
            }

            //update by gordon 20151020 U247
            //if (ddlStation.Items.FindByValue(this.ODMUser.StationID.ToString()) != null && Request.QueryString["action"].ToUpper() == "NEW")
            //{
            //    this.ddlStation.SelectedValue = this.ODMUser.StationID.ToString();
            //}
            if (ddlStation.Items.FindByValue(this.ODMUser.LocationID.ToString()) != null && Request.QueryString["action"].ToUpper() == "NEW")
            {
                this.ddlStation.SelectedValue = this.ODMUser.LocationID.ToString();
            }
        }

        //set the Edit or New parameters value.
        private void SetParameters()
        {
            //set the parameters.
            assetTypeID = Request.QueryString["AssetTypeID"] != null ? Convert.ToInt32(Request.QueryString["AssetTypeID"].ToString()) : 0;
            locationID = ddlStation.SelectedItem != null ? Convert.ToInt32(ddlStation.SelectedValue) : 0;
            assetTypeCode = this.txtAssetType.Text.Trim();
            assetTypeDesc = this.txtDescription.Text.Trim();
            flagObsolete = this.chkObsolete.Checked ? 1 : 0;
            createUser = this.ODMUser.ID;
            lastUpdUser = this.ODMUser.ID;
            actingasUser = this.ODMUser.ID;
        }

        //Set All Controls Disable.
        private void DisableAllControls()
        {
            this.txtAssetType.Enabled = false;
            this.txtDescription.Enabled = false;
            this.chkObsolete.Enabled = false;
            this.ddlStation.Enabled = false;

            this.btnSave.Enabled = false;
            this.btnSaveAndNew.Enabled = false;
            this.btnSaveAndReturn.Enabled = false;
        }

        //Check asset type can void.
        private string CheckVoid()
        {
            bool check = true;
            string tempString = "";
            //u44 update by benson 2015-08-18 begin
            //if (Request.QueryString["AssetTypeID"] != null && Request.QueryString["AssetTypeID"].Trim().Length > 0)
            //{
            //    check = DAL.SOM.CodeSetup.CDAssetType.VoidCheck(Convert.ToInt32(Request.QueryString["AssetTypeID"].Trim()));
            //    if (!check)
            //    {
            //        tempString = GetMessage("SOM_AssetTypeUsed");
            //    }
            //}
            //u44 update by benson 2015-08-18 end
            return tempString;
        }

        #endregion

        #region Some Event.

        //Clear the Text on all TextBox.
        private void ClearText()
        {
            this.txtAssetType.Text = "";
            this.txtDescription.Text = "";
        }

        //Save Check
        protected bool SaveCheck()
        {
            bool check = true;

            if (this.txtAssetType.Text.Trim().Length < 1)
            {
                this.Alert(GetMessage("SOM_AssetTypeNotNull"));

                check = false;
            }

            if (this.ddlStation.SelectedItem == null)
            {
                this.Alert("'" + this.lblStation.Text + "' " + GetMessage("SOM_NotIsEmpty", this.LangCode));

                check = false;
            }

            //void check.
            //if (this.chkObsolete.Checked && ODMS.DAL.SOM.CodeSetup.CDAssetItem.GetAssetItemList(0, "", this.assetTypeID, "", "", false).Rows.Count > 0)
            //{
            //    check = false;

            //    this.Alert(GetMessage("SOM_AssetTypeUsed"));
            //}

            return check;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!SaveCheck())
            {
                return;
            }

            SetParameters();

            int isSucces;
            if ( Request.QueryString["AssetTypeID"] != null && Request.QueryString["action"].ToUpper() == "EDIT")
            {
                isSucces = MTRC.ODMS.DAL.SOM.CodeSetup.CDAssetType.UpdateAssetType(assetTypeID, locationID, assetTypeCode, assetTypeDesc, flagObsolete, lastUpdUser);
            }
            else
            {
                int[] temp = MTRC.ODMS.DAL.SOM.CodeSetup.CDAssetType.AddAssetType(locationID, assetTypeCode, assetTypeDesc, flagObsolete, createUser, lastUpdUser, actingasUser);
                isSucces = temp[0];
                assetTypeID = temp[1];
            }

            if (isSucces != 1)
            {
                this.Alert(GetMessage("SOM_ExistAssetType", this.LangCode));
            }
            else
            {
                Response.Redirect("AssetTypeEntry.aspx?action=edit&AssetTypeID=" + assetTypeID);
            }
        }

        protected void btnSaveAndReturn_Click(object sender, EventArgs e)
        {
            if (!SaveCheck())
            {
                return;
            }

            SetParameters();

            int isSucces;
            if ( Request.QueryString["AssetTypeID"] != null && Request.QueryString["action"].ToUpper() == "EDIT")
            {
                isSucces = MTRC.ODMS.DAL.SOM.CodeSetup.CDAssetType.UpdateAssetType(assetTypeID, locationID, assetTypeCode, assetTypeDesc, flagObsolete, lastUpdUser);
            }
            else
            {
                isSucces = MTRC.ODMS.DAL.SOM.CodeSetup.CDAssetType.AddAssetType(locationID, assetTypeCode, assetTypeDesc, flagObsolete, createUser, lastUpdUser, actingasUser)[0];
            }

            if (isSucces != 1)
            {
                this.Alert(GetMessage("SOM_ExistAssetType", this.LangCode));
            }
            else
            {
                Response.Redirect("AssetTypeList.aspx");
            }
        }

        protected void btnSaveAndNew_Click(object sender, EventArgs e)
        {
            if (!SaveCheck())
            {
                return;
            }

            SetParameters();

            int isSucces;
            if ( Request.QueryString["AssetTypeID"] != null && Request.QueryString["action"].ToUpper() == "EDIT")
            {
                isSucces = MTRC.ODMS.DAL.SOM.CodeSetup.CDAssetType.UpdateAssetType(assetTypeID, locationID, assetTypeCode, assetTypeDesc, flagObsolete, lastUpdUser);
            }
            else
            {
                isSucces = MTRC.ODMS.DAL.SOM.CodeSetup.CDAssetType.AddAssetType(locationID, assetTypeCode, assetTypeDesc, flagObsolete, createUser, lastUpdUser, actingasUser)[0];
            }


            if (isSucces != 1)
            {
                this.Alert(GetMessage("SOM_ExistAssetType", this.LangCode));
            }
            else
            {
                Response.Redirect("AssetTypeEntry.aspx?action=new");
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("AssetTypeList.aspx");
        }

        #endregion
    }
}
